//
//  TableViewCustomView.m
//  zhijianshejian
//
//  Created by admin on 16/5/18.
//  Copyright © 2016年 haoniu-duhuan. All rights reserved.
//

#import "TableViewCustomView.h"

#define TableViewCustomView_tableViewCellHeight 50  //cell高度
#define TableViewCustomView_tableViewShowMaxCellCount 5 //最多显示cell数量
#define TableViewCustomView_contentViewLineBackColor [UIColor redColor] //[AppConfig colorWithHexString:@"#ff9a57"] //标题下横线的颜色
#define TableViewCustomView_contentViewFootCanMove false //foot视图是否可移动
@implementation TableViewCustomView{
    
}

static TableViewCustomView *showCustomView;

+(instancetype) shareInstance;{
    if(!showCustomView){
        showCustomView=[[TableViewCustomView alloc] init];
        [showCustomView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.2]];//colorWithWhite:0 alpha:1
        //阴影效果
        [showCustomView.layer setShadowOpacity:0.6];
        [showCustomView.layer setShadowColor:[UIColor blackColor].CGColor];
    }else{
        for (UIView *view in showCustomView.subviews) {
            [view removeFromSuperview];
        }
        for (UIView *view in showCustomView.headView.subviews) {
            [view removeFromSuperview];
        }
    }
    //初始化内容视图
    showCustomView.contentView = [[UIView alloc]init];
    [showCustomView addSubview:showCustomView.contentView];
    
    //初始化tableview
    showCustomView.tableView = [[UITableView alloc]init];
    [showCustomView.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [showCustomView.contentView addSubview:showCustomView.tableView];
    showCustomView.dataSource = [NSMutableArray array];
    [showCustomView setShowEnum:ENUM_SHowCustonViewCenter];
    [showCustomView setSelectEnum:ENUM_SHowCustonSelectRedio];
    
    [showCustomView setFrame:[UIApplication sharedApplication].keyWindow.bounds];
    return showCustomView;
}
+(instancetype) initWithTitle:(NSString *)title withDataSource:(NSArray *)dataSource withPosition:(ENUM_ShowCustomView)position withSelect:(ENUM_ShowCustomViewSelect)select withDelegate:(id)delegate{
    showCustomView = [TableViewCustomView shareInstance];
    
    if (dataSource!=nil && dataSource.count>0) {
        showCustomView.viewTitle = title;
        showCustomView.dataSource = [NSMutableArray arrayWithArray:dataSource];
        showCustomView.ShowEnum = position;
        showCustomView.selectEnum = select;
        showCustomView.delegate = delegate;
        
        if (position == ENUM_SHowCustonViewTop) {
            [showCustomView showHeadView];
        }else if(position == ENUM_SHowCustonViewFooter) {
            [showCustomView showFootView];
        }else {
            [showCustomView showCenterView];
        }
    }
    return showCustomView;
}
//顶部视图
-(void)showHeadView{
    [showCustomView.layer setShadowRadius:2];
    [showCustomView.layer setShadowOffset:CGSizeMake(0, 2)];//调整阴影位置
    [showCustomView.contentView.layer setMasksToBounds:NO];
    [showCustomView.contentView.layer setCornerRadius:0];
    [showCustomView.contentView setFrame:CGRectMake(0, 0, self.frame.size.width, 0)];
    
    showCustomView.contentViewHeight = (int)showCustomView.dataSource.count>=TableViewCustomView_tableViewShowMaxCellCount ? TableViewCustomView_tableViewShowMaxCellCount*TableViewCustomView_tableViewCellHeight : (int)showCustomView.dataSource.count*TableViewCustomView_tableViewCellHeight;
    [showCustomView.tableView setFrame:CGRectMake(0, 0, self.frame.size.width, 0)];
    showCustomView.tableView.dataSource = self;
    showCustomView.tableView.delegate = self;
    [showCustomView.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [showCustomView.tableView reloadData];
}
//中间视图
-(void)showCenterView{
    [showCustomView.layer setShadowRadius:4];
    [showCustomView.layer setShadowOffset:CGSizeMake(0, 0)];//调整阴影位置
    [showCustomView.contentView.layer setMasksToBounds:YES];
    [showCustomView.contentView.layer setCornerRadius:5];
    
    showCustomView.contentViewHeight = (int)showCustomView.dataSource.count>=TableViewCustomView_tableViewShowMaxCellCount ? TableViewCustomView_tableViewShowMaxCellCount*TableViewCustomView_tableViewCellHeight : (int)showCustomView.dataSource.count*TableViewCustomView_tableViewCellHeight;
    CGRect tableRect = CGRectMake(0, 0, showCustomView.frame.size.width, showCustomView.contentViewHeight);
    if (_viewTitle != nil && _viewTitle.length > 0) {
        showCustomView.contentViewHeight+=52;
        tableRect.origin.y = 52;
        //头部标题视图
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width-40, 52)];
        [_headView setBackgroundColor:[UIColor whiteColor]];
        UILabel * headTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, _headView.frame.size.width, 50)];
        [headTitle setText:_viewTitle];
        [headTitle setFont:[UIFont boldSystemFontOfSize:19]];
        [headTitle setTextColor:[UIColor blackColor]];
        [headTitle setTextAlignment:NSTextAlignmentCenter];
        [_headView addSubview:headTitle];
        UILabel * headLine = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, _headView.frame.size.width, 2)];
        [headLine setBackgroundColor:TableViewCustomView_contentViewLineBackColor];
        [_headView addSubview:headLine];
        [showCustomView.contentView addSubview:_headView];
    }
    if (_selectEnum == ENUM_SHowCustonSelectCheckBox) {
        //底部按钮视图
        UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, showCustomView.contentViewHeight, self.frame.size.width-40, 47)];
        UIButton * cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 1, footView.frame.size.width/2, 47)];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(hideByAnimaDefault) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [cancelButton setBackgroundColor:[UIColor whiteColor]];
        [footView addSubview:cancelButton];
        
        UIButton * determineButton = [[UIButton alloc]initWithFrame:CGRectMake(footView.frame.size.width/2+0.5, 1, footView.frame.size.width/2, 47)];
        [determineButton setTitle:@"确定" forState:UIControlStateNormal];
        [determineButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [determineButton addTarget:self action:@selector(hideByAnimaDefaultByCallBack) forControlEvents:UIControlEventTouchUpInside];
        [determineButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [determineButton setBackgroundColor:[UIColor whiteColor]];
        [footView addSubview:determineButton];
        [showCustomView.contentView addSubview:footView];
        
        showCustomView.contentViewHeight+=47;
    }
    [showCustomView.tableView setFrame:tableRect];
    showCustomView.tableView.dataSource = self;
    showCustomView.tableView.delegate = self;
    [showCustomView.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [showCustomView.tableView reloadData];
    
    [showCustomView.contentView setFrame:CGRectMake(20, self.frame.size.height/2-showCustomView.contentViewHeight/2, self.frame.size.width-40, showCustomView.contentViewHeight)];
}
//底部视图
-(void)showFootView{
    [showCustomView.layer setShadowRadius:4];
    [showCustomView.layer setShadowOffset:CGSizeMake(0, 0)];//调整阴影位置
    [showCustomView.contentView.layer setMasksToBounds:NO];
    [showCustomView.contentView.layer setCornerRadius:0];
    [showCustomView.contentView setFrame:CGRectMake(0, showCustomView.frame.size.height, self.frame.size.width, 0)];
    
    showCustomView.contentViewHeight = (int)showCustomView.dataSource.count>=TableViewCustomView_tableViewShowMaxCellCount ? TableViewCustomView_tableViewShowMaxCellCount*TableViewCustomView_tableViewCellHeight : (int)showCustomView.dataSource.count*TableViewCustomView_tableViewCellHeight;
    CGRect tableRect = CGRectMake(0, 0, self.frame.size.width, showCustomView.contentViewHeight);
    //头部标题视图
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, TableViewCustomView_contentViewFootCanMove?62:52)];
    [_headView setBackgroundColor:[UIColor whiteColor]];
    if (_viewTitle != nil && _viewTitle.length > 0) {
        UILabel * headTitle = [[UILabel alloc]initWithFrame:CGRectMake(80, 0, _headView.frame.size.width-160, 50)];
        [headTitle setText:_viewTitle];
        [headTitle setFont:[UIFont boldSystemFontOfSize:19]];
        [headTitle setTextColor:[UIColor blackColor]];
        [headTitle setTextAlignment:NSTextAlignmentCenter];
        [_headView addSubview:headTitle];
        UILabel * headLine = [[UILabel alloc]initWithFrame:CGRectMake(0, _headView.frame.size.height-2, _headView.frame.size.width, 2)];
        [headLine setBackgroundColor:TableViewCustomView_contentViewLineBackColor];
        [_headView addSubview:headLine];
        
        if (TableViewCustomView_contentViewFootCanMove) {
            UIImageView * moveImage = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width/2-25, 40, 50, 15)];
            [moveImage setImage:[UIImage imageNamed:@"icon_move1"]];
            [_headView addSubview:moveImage];
        }
        
        [showCustomView.contentView addSubview:_headView];
    }
    if (_selectEnum == ENUM_SHowCustonSelectCheckBox) {
        UIButton * cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 60, TableViewCustomView_contentViewFootCanMove?40:30)];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [cancelButton addTarget:self action:@selector(hideByAnimaDefault) forControlEvents:UIControlEventTouchUpInside];
        [cancelButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [cancelButton setBackgroundColor:[AppConfig colorWithHexString:@"#e8e8e8"]];
        [cancelButton.layer setMasksToBounds:YES];
        [cancelButton.layer setCornerRadius:3];
        [_headView addSubview:cancelButton];
        
        UIButton * determineButton = [[UIButton alloc]initWithFrame:CGRectMake(_headView.frame.size.width-70, 10, 60, TableViewCustomView_contentViewFootCanMove?40:30)];
        [determineButton setTitle:@"确定" forState:UIControlStateNormal];
        [determineButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [determineButton addTarget:self action:@selector(hideByAnimaDefaultByCallBack) forControlEvents:UIControlEventTouchUpInside];
        [determineButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [determineButton setBackgroundColor:[AppConfig colorWithHexString:@"#e8e8e8"]];
        [determineButton.layer setMasksToBounds:YES];
        [determineButton.layer setCornerRadius:3];
        [_headView addSubview:determineButton];
        
        [showCustomView.contentView addSubview:_headView];
    }
    if ((_viewTitle != nil && _viewTitle.length > 0) || _selectEnum == ENUM_SHowCustonSelectCheckBox){
        showCustomView.contentViewHeight+=TableViewCustomView_contentViewFootCanMove?62:52;
        tableRect.origin.y = TableViewCustomView_contentViewFootCanMove?62:52;
    }
    [showCustomView.tableView setFrame:tableRect];
    showCustomView.tableView.dataSource = self;
    showCustomView.tableView.delegate = self;
    [showCustomView.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [showCustomView.tableView reloadData];
}

#pragma -mark TableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TableViewCustomView_tableViewCellHeight;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [_delegate tableViewCustomView:self cellForRowAtIndexPath:indexPath];
    
    UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(0, TableViewCustomView_tableViewCellHeight-0.5, self.frame.size.width, 0.5)];
    [line setBackgroundColor:[AppConfig colorWithHexString:@"#cacaca"]];
    [cell addSubview:line];
    
//    [cell setAccessoryView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_dui"]]];
//    [cell.accessoryView setHidden:NO];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_delegate tableViewCustomView:self didSelectRowAtIndexPath:indexPath];
}


#pragma -mark touch
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    if ([touch view]==self) {
        [self hideByAnimaDefault];
    }else{
        if ([touch view]==_headView && [self ShowEnum] == ENUM_SHowCustonViewFooter && TableViewCustomView_contentViewFootCanMove) {
            CGPoint pint = [touch locationInView:[touch view]];
            _startPointY = pint.y;
            NSLog(@"==_startPointY:%d",_startPointY);
        }
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UIView * contentView = [self subviews][0];
    UITouch * touch = [touches anyObject];
    if ([touch view]==_headView && [self ShowEnum] == ENUM_SHowCustonViewFooter && TableViewCustomView_contentViewFootCanMove) {
        CGPoint pint = [touch locationInView:[touch view]];
        if (pint.y<=55 && !_startMoving) {
            _startMoving = YES;
        }
        BOOL bol = self.frame.size.height<pint.y+contentView.frame.origin.y-_startPointY+self.contentViewHeight;
        if (_startMoving && bol) {
            //NSLog(@"=====pint.y:%f",pint.y);
            [contentView setFrame:CGRectMake(contentView.frame.origin.x, pint.y+contentView.frame.origin.y-_startPointY, contentView.frame.size.width, contentView.frame.size.height)];
        }
    }
    
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UIView * contentView = [self subviews][0];
    UITouch * touch = [touches anyObject];
    if ([touch view]==_headView && [self ShowEnum] == ENUM_SHowCustonViewFooter && TableViewCustomView_contentViewFootCanMove) {
        _startMoving = NO;
        _startPointY = 0;
        int centerline = self.frame.size.height-self.contentViewHeight/2;
        if (contentView.frame.origin.y>=centerline) {
            double t = self.contentViewHeight/45*0.1;
            [UIView animateWithDuration:t animations:^(void){
                UIView * contentView = [self subviews][0];
                [contentView setFrame:CGRectMake(contentView.frame.origin.x, self.frame.size.height, contentView.frame.size.width, self.contentViewHeight)];
            }completion:^(BOOL finishion){
                self.isShown=false;
                [self removeFromSuperview];
            }];
        }else{
            double t = self.contentViewHeight/45*0.1;
            [UIView animateWithDuration:t animations:^(void){
                UIView * contentView = [self subviews][0];
                [contentView setFrame:CGRectMake(contentView.frame.origin.x, self.frame.size.height-contentView.frame.size.height, contentView.frame.size.width, self.contentViewHeight)];
            }];
        }
    }
}

#pragma -mark show Or hide
-(void)hideByAnimaDefaultByCallBack{
    
    [self hideByAnimaDefault];
}
#pragma -mark 视图现实隐藏
-(void)hideByAnimaDefault{
    NSLog(@"====hideByAnimaDefault√:%d",ENUM_SHowCustonViewTop);
    if ([self ShowEnum] == ENUM_SHowCustonViewTop) {
        double t = self.contentViewHeight/45*0.1;
        [UIView animateWithDuration:t animations:^(void){
            [self.contentView setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 0)];
            [self.tableView setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 0)];
        }completion:^(BOOL finishion){
            self.isShown=false;
            [self removeFromSuperview];
        }];
    }else if ([self ShowEnum] == ENUM_SHowCustonViewFooter) {
        double t = self.contentViewHeight/45*0.1;
        [UIView animateWithDuration:t animations:^(void){
            [self.contentView setFrame:CGRectMake(0, self.frame.size.height, self.contentView.frame.size.width, 0)];
        }completion:^(BOOL finishion){
            self.isShown=false;
            [self removeFromSuperview];
        }];
    }else{
        //直接显示不需要动画效果
        self.isShown=false;
        [self removeFromSuperview];
    }
    
}
-(void)showByAnimaDefault{
    NSLog(@"====showByAnimaDefault:%d",(int)self.contentView.frame.size.height);
    self.isShown=YES;
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    if ([self ShowEnum] == ENUM_SHowCustonViewTop) {
        double t = self.contentViewHeight/45*0.1;
        NSLog(@"====t:%f",t);
        [UIView animateWithDuration:t animations:^(void){
            [self.contentView setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentViewHeight)];
            [self.tableView setFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentViewHeight)];
        }];
    }else if ([self ShowEnum] == ENUM_SHowCustonViewFooter) {
        double t = self.contentViewHeight/45*0.1;
        [UIView animateWithDuration:t animations:^(void){
            [self.contentView setFrame:CGRectMake(0, self.frame.size.height-self.contentViewHeight, self.contentView.frame.size.width, self.contentViewHeight)];
        }];
    }else{
        //直接显示不需要动画效果
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
